﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PetaPoco;
using Shop.Order.Models;

namespace Shop.Order.Repositories
{
    /// <summary>
    /// 
    /// </summary>
    public class CourierAccess
    {
        private Database DB = new Database("ConnString");
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<Models.CourierCategory> GetCCInfo()
        {
            Sql sql = Sql.Builder.Append("select * from Shop_CourierCategory");
            return DB.Fetch<CourierCategory>(sql);
        }
        /// <summary>
        /// 添加类别
        /// </summary>
        /// <param name="ccinfo"></param>
        /// <returns></returns>
        public object AddCourierCategory(CourierCategory ccinfo)
        {
            return DB.Insert("Shop_CourierCategory", "CCID", ccinfo);
        }
        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="CCID"></param>
        /// <returns></returns>
        public int DelCourierCategory(int CCID)
        {
            Sql sql = Sql.Builder.Append("delete from Shop_CourierCategory where CCID = @0",CCID);
            return DB.Execute(sql);
        }
        /// <summary>
        /// ID
        /// </summary>
        /// <param name="CCID"></param>
        /// <returns></returns>
        public IList<Models.Courier> GetCCount(int CCID)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_Courier where CCID = @0",CCID);
            return DB.Fetch<Courier>(sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CCID"></param>
        /// <returns></returns>
        public int UpdateCourierCategory(CourierCategory ccinfo)
        {
            Sql sql = Sql.Builder.Append("update Shop_CourierCategory set CName = @0,CReg = @1 where CCID = @2", ccinfo.CName,ccinfo.CReg,ccinfo.CCID);
            return DB.Execute(sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CName"></param>
        /// <returns></returns>
        public CourierCategory VerificationCName(string CName)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_CourierCategory where CName = @0", CName);
            return DB.FirstOrDefault<CourierCategory>(sql);
        }
        /// <summary>
        /// ID
        /// </summary>
        /// <param name="CCID"></param>
        /// <returns></returns>
        public CourierCategory GetCCInfo(int CCID)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_CourierCategory where CCID = @0", CCID);
            return DB.FirstOrDefault<CourierCategory>(sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CourierNumber"></param>
        /// <returns></returns>
        public Courier GetCInfo(string CourierNumber)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_Courier where CourierNumber = @0", CourierNumber);
            return DB.FirstOrDefault<Courier>(sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CCode"></param>
        /// <returns></returns>
        public CourierCategory GetCCInfo(string CCode)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_CourierCategory where CCode = @0", CCode);
            return DB.FirstOrDefault<CourierCategory>(sql);
        }
        //
        public object AddCourier(Courier cinfo)
        {
            return DB.Insert("Shop_Courier", "CID", cinfo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Page">第几页</param>
        /// <param name="PageNum">每页多少条</param>
        /// <param name="Status">订单状态</param>
        /// <param name="Strart">开始时间</param>
        /// <param name="End">结束时间</param>
        /// <param name="SearchType">搜索类型</param>
        /// <param name="Search">搜索内容</param>
        /// <param name="order">排序方式</param>
        /// <param name="CCode"></param>
        /// <returns></returns>
        public Page<Models.AnyCourier> GetCinfo(long Page, long PageNum, int Status, string Strart, string End, int SearchType, string Search, int order,string CCode)
        {
            string strsql = string.Format("select Shop_Courier.*,Shop_CourierCategory.CName from Shop_Courier join Shop_CourierCategory on Shop_CourierCategory.CCode = Shop_Courier.CCode");
            string WhatSearch = "";
            switch (SearchType)
            {
                case 0:
                    WhatSearch = "CourierNumber";
                    break;
                default:
                    break;
            }
            strsql += string.Format(" where Shop_Courier.{0} like '%{1}%'", WhatSearch, Search);

            //这里把9看为全部
            if (Status != 9)
            {
                strsql += string.Format("and Shop_Courier.Status = {0} ", Status);
            }
            //时间
            if (!string.IsNullOrEmpty(Strart))
            {
                strsql += string.Format("and Shop_Courier.CreateDate > '{0}' and Shop_Courier.CreateDate < '{1}' ", Strart, End);
            }
            //
            if (CCode != "9")
            {
                strsql += string.Format("and Shop_Courier.CCode = '{0}' ", CCode);
            }
            //这里把0为desc为正序1为倒序
            if (order == 0)
            {
                strsql += string.Format(" order by Shop_Courier.CreateDate desc");
            }
            Sql sql = Sql.Builder.Append(strsql);
            return DB.Page<Models.AnyCourier>(Page, PageNum, sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CID">ID</param>
        /// <returns></returns>
        public int DelCourier(int CID)
        {
            Sql sql = Sql.Builder.Append("delete from Shop_Courier where CID = @0", CID);
            return DB.Execute(sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CCode"></param>
        /// <returns></returns>
        public IList<Courier> VCourierCategory(string CCode)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_Courier where CCode = @0 and Status =0", CCode);
            return DB.Fetch<Courier>(sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CCode"></param>
        /// <returns></returns>
        public Courier GetCurier(string CCode)
        {
            Sql sql = Sql.Builder.Append("select top 10 * from Shop_Courier where Status = 0 and CCode = @0", CCode);
            return DB.FirstOrDefault<Courier>(sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CourierNumber"></param>
        /// <returns></returns>
        public int UseCourier(string CourierNumber)
        {
            Sql sql = Sql.Builder.Append("update Shop_Courier set Status = 1 where CourierNumber = @0", CourierNumber);
            return DB.Execute(sql);
        }
    }
}
